import Login from './login';
import Util from './utils';
import Session from './session';
import Request from './request';
import File from './file';
import PayMent from './payment';
import Cache from './cache';
import moderation from './moderation';
import decrypt from './decrypt';
import log from './log';
import stat from './stat';

class Muni {
    constructor() {
        this.login = Login;
        this.util = Util;
        this.request = Request;
        this.file = File;
        this.payment = PayMent;
        this.cache = Cache;
        this.moderation = moderation;
        this.decrypt = decrypt;
        this.log = log;
        this.stat = stat;
    }

    setAppInfo(appInfo) {
        let newAppid = '';

        // uni.getAccountInfoSync()有兼容性问题，分平台处理
        // https://uniapp.dcloud.net.cn/api/other/getAccountInfoSync.html#getaccountinfosync

        /* #ifdef MP-WEIXIN || MP-QQ || MP-KUAISHOU || MP-JD || MP-360 || MP-ALIPAY */
        newAppid = uni.getAccountInfoSync().miniProgram.appId;
        /* #endif */

        /* #ifdef MP-BAIDU || MP-TOUTIAO || MP-LARK */
        if (!appInfo) throw new Error('missing paramter appInfo');

        if (Util.getType(appInfo) === 'Object') {
            if (Object.values(appInfo).length >= 2) {
                newAppid = appInfo.appid;
            } else {
                throw new Error('missing paramter appid and platform');
            }
        } else if (Util.getType(appInfo) === 'Array') {
            let newAppInfo = appInfo.filter(
                (item) => item.platform === uni.getSystemInfoSync().uniPlatform
            );
            newAppid = newAppInfo[0].appid;
        }
        /* #endif */

        Session.setAppid(newAppid);

        return newAppid;
    }
}

const muni = new Muni();

export default muni;
